﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Security.Principal;

using IfSoft.OA.Entities;
using IfSoft.OA.Business;

namespace IfSoft.OA.WebPortal.Controllers
{ 
    [Authorize]
    public class BankBillController : Controller
    {
        private BankBillManager manager = new BankBillManager();

        //
        // GET: /BankBill/

        public ViewResult Index()
        {
           bool flag = User.IsInRole("BankBillStep1");
            BankBillCriteria criteria = new BankBillCriteria();

            #region Initialize search conditions
            string temp = string.Empty;
            int pageIndex = 0;
            if (!string.IsNullOrEmpty(Request["pageIndex"]))
            {
                temp = Request["pageIndex"];
                Int32.TryParse(temp, out pageIndex);
            }
            if (pageIndex > 0)
                criteria.PageNumber = pageIndex;

            if (!string.IsNullOrEmpty(Request["payCompany_S"]))
            {
                temp = Request["payCompany_S"];
                criteria.PayCompany = temp;
                ViewBag.PayCompany_S = temp;
            }

            if (!string.IsNullOrEmpty(Request["payBank_S"]))
            {
                temp = Request["payBank_S"];
                criteria.PayBank = temp;
                ViewBag.payBank_S = temp;
            }
          
            if (!string.IsNullOrEmpty(Request["payType_S"]))
            {
                temp = Request["payType_S"];
                criteria.PayType = temp;
                ViewBag.PayType_S = temp;
            }
            ViewBag.PayTypes = GetPayTypeSelectList(criteria.PayType, "所有类型");

            if (!string.IsNullOrEmpty(Request["payNoteNo_S"]))
            {
                temp = Request["payNoteNo_S"];
                criteria.PayNoteNo = temp;
                ViewBag.payNoteNo_S = temp;
            }

            if (!string.IsNullOrEmpty(Request["receiveCompany_S"]))
            {
                temp = Request["receiveCompany_S"];
                criteria.ReceiveCompany = temp;
                ViewBag.receiveCompany_S = temp;
            }

            if (!string.IsNullOrEmpty(Request["receiveBank_S"]))
            {
                temp = Request["receiveBank_S"];
                criteria.ReceiveBank = temp;
                ViewBag.receiveBank_S = temp;
            }

            if (!string.IsNullOrEmpty(Request["receiveAccountNo_S"]))
            {
                temp = Request["receiveAccountNo_S"];
                criteria.ReceiveAccountNo = temp;
                ViewBag.receiveAccountNo_S = temp;
            }
            #endregion

            PagedList<BankBill> result = manager.SearchPagedList(criteria);
            ViewBag.PageCount = result.PageCount;
            ViewBag.CurrentPage = result.CurrentPage;            

            return View(result);
        }

        //
        // GET: /BankBill/Details/5

        public ViewResult Details(int id)
        {
            BankBill bankbill = manager.SearchById(id);            
            return View(bankbill);
        }

        //
        // GET: /BankBill/Create

        public ActionResult Create()
        {
            ViewBag.PayTypes = GetPayTypeSelectList(string.Empty, string.Empty);
            ViewBag.PayNoteTypes = GetPayNoteTypeSelectList("发票", string.Empty);
            ViewBag.Currencies = GetCurrencySelectList(string.Empty, string.Empty);
            return View();
        } 

        //
        // POST: /BankBill/Create

        [HttpPost]
        public ActionResult Create(BankBill bankbill)
        {
            if (ModelState.IsValid)
            {
                bankbill.CreatedTime = DateTime.Now;
                bankbill.Creator = User.Identity.Name;
                               
                string tempDate = string.Empty;
                string tempDescription = string.Empty;
                for (int i = 1; i <= 4; i++)
                {
                    tempDate = Request["KeyDate" + i];
                    tempDescription = Request["DetailDescription" + i];
                    if (!string.IsNullOrEmpty(tempDate))
                    {
                        BankBillDetail detail = new BankBillDetail();
                        detail.KeyDate = DateTime.Parse(tempDate);
                        if (!string.IsNullOrEmpty(tempDescription))
                            detail.Description = tempDescription;
                        bankbill.BankBillDetails.Add(detail);
                    }
                }
               
                manager.Create(bankbill);            
                return RedirectToAction("Index");  
            }

            return View(bankbill);
        }
        
        //
        // GET: /BankBill/Edit/5
 
        public ActionResult Edit(int id)
        {
            BankBill bankbill = manager.SearchById(id);
            ViewBag.PayTypes = GetPayTypeSelectList(bankbill.PayType, string.Empty);
            ViewBag.PayNoteTypes = GetPayNoteTypeSelectList(bankbill.PayNoteType, string.Empty);
            ViewBag.Currencies = GetCurrencySelectList(bankbill.Currency, string.Empty);
            return View(bankbill);
        }

        //
        // POST: /BankBill/Edit/5

        [HttpPost]
        public ActionResult Edit(BankBill bankbill)
        {
            if (ModelState.IsValid)
            {
                BankBill dbBankBill = manager.SearchById(bankbill.Id);
                bankbill.CreatedTime = dbBankBill.CreatedTime;
                bankbill.Creator = dbBankBill.Creator;

                manager.Update(bankbill);
                return RedirectToAction("Index");
            }
            return View(bankbill);
        }

        //
        // GET: /BankBill/Delete/5
 
        public ActionResult Delete(int id)
        {
            BankBill bankbill = manager.SearchById(id);
            return View(bankbill);
        }

        //
        // POST: /BankBill/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            manager.Delete(id);
            //BankBill bankbill = manager.SearchById(id);
            //db.BankBills.Remove(bankbill);
            //db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {            
            base.Dispose(disposing);
        }

        public ActionResult Submit(int id)
        {
            BankBill bo = manager.SearchById(id);

            string userName = User.Identity.Name;
           
            bo.Step1 = userName;
            bo.Step1Date = DateTime.Now;
            manager.Update(bo);
            return RedirectToAction("Index");
        }

        public ActionResult Approve(int id)
        {
            BankBill bo = manager.SearchById(id);
            
            string userName = User.Identity.Name;
            DateTime stepDate = DateTime.Now;

            #region role check
            //if (user.IsInRole(BusinessRoles.BankBillStep1))
            //{
            //    bo.Step1 = userName;
            //    bo.Step1Date = stepDate;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep2))
            //{
            //    bo.Step2 = userName;
            //    bo.Step2Date = stepDate;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep3))
            //{
            //    bo.Step3 = userName;
            //    bo.Step3Date = stepDate;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep4))
            //{
            //    bo.Step4 = userName;
            //    bo.Step4Date = stepDate;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep5))
            //{
            //    bo.Step5 = userName;
            //    bo.Step5Date = stepDate;
            //}
            //else
            //{
            //    throw new Exception("Invalid Role.");
            //}
            #endregion

            if (userName == "user1")
            {
                bo.Step1 = userName;
                bo.Step1Date = stepDate;
            }
            else if (userName == "user2")
            {
                bo.Step2 = userName;
                bo.Step2Date = stepDate;
            }
            else if (userName == "user3")
            {
                bo.Step3 = userName;
                bo.Step3Date = stepDate;
            }
            else if (userName == "user4")
            {
                bo.Step4 = userName;
                bo.Step4Date = stepDate;
            }
            else if (userName == "user5")
            {
                bo.Step5 = userName;
                bo.Step5Date = stepDate;
            }
            else
            {
                throw new Exception("Invalid Role.");
            }
            manager.Update(bo);
            return RedirectToAction("Index");  
        }

        public ActionResult Deny(int id)
        {
            BankBill bo = manager.SearchById(id);
            
            string userName = User.Identity.Name;
            DateTime stepDate = DateTime.Now;

            #region role check
            //if (user.IsInRole(BusinessRoles.BankBillStep1))
            //{
            //    throw new Exception("Can not deny at first step.");
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep2))
            //{
            //    bo.Step1 = string.Empty;
            //    bo.Step1Date = null;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep3))
            //{
            //    bo.Step2 = string.Empty;
            //    bo.Step2Date = null;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep4))
            //{
            //    bo.Step3 = string.Empty;
            //    bo.Step3Date = null;
            //}
            //else if (user.IsInRole(BusinessRoles.BankBillStep5))
            //{
            //    bo.Step4 = string.Empty;
            //    bo.Step4Date = null;
            //}
            //else
            //{
            //    throw new Exception("Invalid step for deny.");
            //}
            #endregion

            #region role check
            if (userName == "user1")
            {
                throw new Exception("Can not deny at first step.");
            }
            else if (userName == "user2")
            {
                bo.Step1 = string.Empty;
                bo.Step1Date = null;
            }
            else if (userName == "user3")
            {
                bo.Step1 = string.Empty;
                bo.Step1Date = null;
                bo.Step2 = string.Empty;
                bo.Step2Date = null;
            }
            else if (userName == "user4")
            {
                bo.Step1 = string.Empty;
                bo.Step1Date = null;
                bo.Step2 = string.Empty;
                bo.Step2Date = null;
                bo.Step3 = string.Empty;
                bo.Step3Date = null;
            }
            else if (userName == "user5")
            {
                bo.Step1 = string.Empty;
                bo.Step1Date = null;
                bo.Step2 = string.Empty;
                bo.Step2Date = null;
                bo.Step3 = string.Empty;
                bo.Step3Date = null;
                bo.Step4 = string.Empty;
                bo.Step4Date = null;
            }
            else
            {
                throw new Exception("Invalid step for deny.");
            }
            #endregion

            manager.Update(bo);
            return RedirectToAction("Index");  
        }

        private SelectList GetPayTypeSelectList(string selectedValue, string allText)
        {           
            Dictionary<string, string> list = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(allText))
                list.Add(string.Empty,allText);
            list.Add("支票","支票");
            list.Add("网银", "网银");
            list.Add("贷记凭证", "贷记凭证");
            list.Add("本票", "本票");
            list.Add("汇票", "汇票");

            return new SelectList(list, "Key", "Value", selectedValue);            
        }

        private SelectList GetPayNoteTypeSelectList(string selectedValue, string allText)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(allText))
                list.Add(string.Empty,allText);
            list.Add("合同", "合同");
            list.Add("发票", "发票");
            list.Add("收据", "收据");
            list.Add("说明", "说明");

            return new SelectList(list, "Key", "Value", selectedValue);       
        }

        private SelectList GetCurrencySelectList(string selectedValue, string allText)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(allText))
                list.Add(string.Empty,allText);
            list.Add("RMB", "RMB");
            list.Add("USD","USD");
         
            return new SelectList(list, "Key", "Value", selectedValue);   
        }
    }
}